﻿module UserLexer

open LinesLexer
open CodeLexer
open Tokens

let prepare_lexer content =
    //let reader = new System.IO.StringReader(content)
    Lexing.LexBuffer<_>.FromString content

let parse_string parser seq_tokens = 
    seq { for token in seq_tokens do
            let lexbuf = prepare_lexer token
            while not lexbuf.IsPastEndOfStream do
              yield parser lexbuf
        }

let divideLines content = 
    let lexbuf = prepare_lexer content
    seq {while not lexbuf.IsPastEndOfStream do
           //Lexer.source := content
           yield LinesLexer.divideLines lexbuf
        }

let distinguishSharpLines seq_tokens =
    seq {for token in seq_tokens do
            match token with
            | LINE x -> //printfn "%A" x
                        let lexbuf = prepare_lexer x
                        while not lexbuf.IsPastEndOfStream do
                          //Lexer.source := x
                          let temp = LinesLexer.distinguishSharpLines lexbuf
                          if temp <> EOF then yield temp
                        //printfn "%A" x
            | x -> yield x
        }

let findTokens seq_tokens = 
    seq {for token in seq_tokens do
            match token with
            | LINE x -> //printfn "%A" x
                        let lexbuf = prepare_lexer x
                        while not lexbuf.IsPastEndOfStream do
                          //Lexer.source := x
                          let temp = CodeLexer.findTokens lexbuf
                          if temp <> EOF then yield temp
                        //printfn "%A" x
            | x -> yield x
    }

let run_string content =
    LinesLexer.linecount <- 1;
    content
    |> divideLines
    |> distinguishSharpLines
    |> findTokens
